package com.mb;

import java.io.File;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

import com.common.constant.FormConstant;
import com.common.util.messages.MessagesController;
import com.facade.MemberFacade;
import com.facade.MembershipPaymentFacade;
import com.facade.RunningNumberFacade;
import com.model.Member;
import com.model.MembershipPayment;
import com.model.User;
import com.objects.MemberSearch;
import com.objects.PaymentSearch;

@ViewScoped
@ManagedBean
public class MembershipPaymentMB extends AbstractMB implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private static Logger log = Logger.getLogger(MembershipPaymentMB.class);
	
	private MembershipPayment membershipPayment;
	private File receipt;
	private StreamedContent receiptContent;
	private Member member;
	private String memberName;
	private Date RenewedExpiryDate;
	private List<MembershipPayment> membershipPaymentList;
	private PaymentSearch paymentSearch;
	
	private MembershipPaymentFacade membershipPaymentFacade; 
	private RunningNumberFacade runningNumberFacade; 
	private MemberFacade memberFacade;
	
	MessagesController msgCtrl = new MessagesController();
	
	public void createMembershipPayment() {
		try {
			
			UserMB userMB = new UserMB();
			User user = userMB.getLoggedUser();
			
			membershipPayment.setPaymentDate(new Date());
			membershipPayment.setActive(true);
			membershipPayment.setMember(member);
			membershipPayment.setMembership(member.getMembership());
			membershipPayment.setFee(member.getMembership().getPrice());
			membershipPayment.setPaymentNo(getRNFacade().getNextRunningNumber(FormConstant.PAYMENT_FORM));
			membershipPayment.setReceivedBy(user);
			
			getMembershipPaymentFacade().createMembershipPayment(membershipPayment);
			getMemberFacade().extendMembership(membershipPayment);
			
			String paymentNo = membershipPayment.getPaymentNo();
			
			resetMembershipPayment();
			closeDialog();
			msgCtrl.addInfo("New Membership Payment Saved", paymentNo + " is saved successfully");
		} catch (Exception e) {
			keepDialogOpen();
			msgCtrl.addFatal("Uhh Ohh", "something is not right here.");
			log.error("Membership Plan error in saving", e);
		}
	}
	
	public MembershipPaymentFacade getMembershipPaymentFacade() {
		if (membershipPaymentFacade == null) {
			membershipPaymentFacade = new MembershipPaymentFacade();
		}		
		return membershipPaymentFacade;
	}
	
	public List<String> activeMemberName(String memberName){
		
		List<String> resultList = new ArrayList<String>();
		List<Member> memberList = getMembershipPaymentFacade().activeMemberAutoComplete(memberName);
		
		for(int i=0;i<memberList.size();i++){
			Member member = memberList.get(i);
			resultList.add(member.getName());
		}
		
		return resultList;
	}
	
	public void setMemberDtl(){
		member = getMemberFacade().findActiveMemberWithName(memberName);
	}
	
	public MemberFacade getMemberFacade() {
		if (memberFacade == null) {
			memberFacade = new MemberFacade();
		}		
		return memberFacade;
	}	
	
	public MembershipPayment getMembershipPayment() {
		if (membershipPayment == null) {
			membershipPayment = new MembershipPayment();
		}
		return membershipPayment;
	}
	
	public void setMembershipPayment(MembershipPayment membershipPayment) {
		this.membershipPayment = membershipPayment;
	}
	
	public void resetMembershipPayment() {
		membershipPayment = new MembershipPayment();
	}

	public RunningNumberFacade getRNFacade(){
		if(runningNumberFacade == null){
			runningNumberFacade = new RunningNumberFacade();
		}
		return runningNumberFacade;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public File getReceipt() {
		return receipt;
	}

	public void setReceipt(File receipt) {
		this.receipt = receipt;
	}

	public String getMemberName() {
		return memberName;
	}

	
	public void setMemberName(String memberName) {
		this.memberName = memberName;
	}

	
	public Date getRenewedExpiryDate() {
		
		if(member != null){
			RenewedExpiryDate = member.getMembershipExpiryDate();
			Calendar c = Calendar.getInstance();
			c.setTime(RenewedExpiryDate);
			c.add(Calendar.MONTH, member.getMembership().getMonthValidity());
			RenewedExpiryDate = c.getTime();
		}
		return RenewedExpiryDate;
	}

	
	public void setRenewedExpiryDate(Date renewedExpiryDate) {
		RenewedExpiryDate = renewedExpiryDate;
	}

	public StreamedContent getReceiptContent() {
		if(membershipPayment != null){
			receipt = getMembershipPaymentFacade().generateReceipt(membershipPayment);
			
			InputStream inputStream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("/jasper/"+receipt.getName());
			receiptContent = new DefaultStreamedContent(inputStream, "application/pdf", "receipt.pdf");
		}

		return receiptContent;
	}

	public void setReceiptContent(StreamedContent receiptContent) {
		this.receiptContent = receiptContent;
	}

	public List<MembershipPayment> getMembershipPaymentList() {
		return membershipPaymentList;
	}
	
	public List<MembershipPayment> getAllMembershipPayment() {
		if(membershipPaymentList == null){
			membershipPaymentList = getMembershipPaymentFacade().listActive();
		}
		
		return membershipPaymentList;
	}

	public void setMembershipPaymentList(
			List<MembershipPayment> membershipPaymentList) {
		this.membershipPaymentList = membershipPaymentList;
	}

	public PaymentSearch getPaymentSearch() {
		if(paymentSearch == null){
			paymentSearch = new PaymentSearch();
		}
		return paymentSearch;
	}

	public void setPaymentSearch(PaymentSearch paymentSearch) {
		this.paymentSearch = paymentSearch;
	}
	
	public void search(){
		membershipPaymentList = getMembershipPaymentFacade().search(paymentSearch);
	}	
	
	
}
